<template>
	<view class="updatePhone">
		<view class="p30 ">
			<view class="input-wrapper">
				<view class="flex-align-center wper100">
					<view class="flex-align-center">
						<view class="desc">
							{{label}}
						</view>
						<view class="line">
							
						</view>
					</view>
				
					<view class="flex1">
						<u-input class="input" v-model="phone" :disabled="disabled" @input="getStatus" placeholder="请输入手机号码"/>
					</view>
				</view>
				
			</view>
			<view class="input-wrapper mt40 flex-between">
				<u-input class="input" v-model="value" @input="getStatus" placeholder="请输入验证码"/>
				<text class="label" @click="sendNoteFun">{{valueLabel}}</text>
			</view>
			
		</view>
		<view class="button-wrapper">
			<view class="button-container">
				<button @click="clickBtn" :disabled="btnDisabled" :class="['button',{'active-button':!btnDisabled}]">{{btnName}}</button>
			</view>

		</view>
	</view>

</template>

<script>
	import {
		sendNoteAPI,
		validateNoteAPI,
		editUserAPI
	} from "@/api/api/user.js"
	import {
		getStoreSyncUtil
	} from "@/utils/store.js"
	import {
		mapState
	} from 'vuex';
	let count = 60;
	let codeLabel = "获取验证码";
	export default {
		data() {
			return {
				value: '',
				id: '',
				phone: '',
				code: "center-old",
				valueLabel: codeLabel,
				valueTimer: 0,
				count: count,
				btnDisabled: true
			}
		},
		computed: {
			...mapState(["loginUser"]),
			label() {
				return this.code == "center-old" ? "当前号码" : "新的号码";
			},
			btnName() {
				return this.code == "center-old" ? "下一步" : "确认修改";
			},
			disabled() {
				return this.code == "center-old" ? true : false;
			},

		},
		onLoad() {
			// #ifdef H5
			let loginUser = getStoreSyncUtil("loginUser");
			this.$store.commit('setLoginUser', loginUser);
			// #endif

			this.phone = this.loginUser.phone;
			//this.phone = "15922565980";
		},
		methods: {
			getStatus() {
				if (!this.$validatenull(this.phone) && !this.$validatenull(this.value)) {
					this.btnDisabled = false;
				}else{
					this.btnDisabled = true;
				}
			},
			sendNoteFun() {
				if (this.valueLabel != codeLabel) return;
				if (!this.valide(true)) return;
				this.valueLabel = count + "秒"
				this.valueTimer = setInterval(() => {
					if (this.count == 1) {
						clearInterval(this.valueTimer);
						this.valueLabel = codeLabel;
						this.count = count;

					} else {
						this.count = this.count - 1;
						this.valueLabel = this.count + `秒`;
					}

				}, 1000)
				sendNoteAPI({
					code: this.code, //"center-change"
					phone: this.phone
				}).then(([err, data]) => {
					if (err) {
						clearInterval(this.valueTimer);
						this.valueLabel = codeLabel;
						this.count = count;
						return;
					};
					this.id = data.id;
				})
			},
			valide(onlyValidPhone = false) {
				if (this.$validatenull(this.phone)) {
					this.$showToast('请输入手机号码');
					return false;
				}
				var reg = /^1[3456789]\d{9}$/;

				if (!reg.test(this.phone)) {
					this.$showToast('请输入正确的手机号码');
					return false;
				}
				//如果是修改验证状态，则验证电话号码和旧手机号码是否一致
				if(this.code == "center-change" && this.phone == this.loginUser.phone){
					this.$showToast('手机号码不能和原手机号码一样');
					return false;
				}
				
			
				if (!this.onlyValidPhone) {
					if (this.$validatenull(this.code)) {
						this.$showToast('请输入验证码');
						return false;
					}
				}

				return true;

			},
			clickBtn() {
				if (this.btnDisabled) return;
				if (!this.valide()) return;
				if (this.code == "center-old") {
					this.validateNoteFun(() => {
						clearInterval(this.valueTimer);
						this.code = "center-change";
						this.valueLabel = codeLabel;
						this.phone = "";
						this.value = "";
						this.id = "";
						this.count = count;
					})
				} else {
					this.validateNoteFun(() => {
						this.editUserFun();
					})
				}
			},
			validateNoteFun(callback) {
				validateNoteAPI({
					value: this.value,
					id: this.id,
					phone: this.phone,
					code: this.code
				}).then(([err, data]) => {
					if (err) return;
					callback();
				})
			},
			$navigateBackDuration(delta, duration = 1500) {
				setTimeout(() => {
					uni.navigateBack({
						delta: delta
					})
				}, duration);
			},
			editUserFun() {
				editUserAPI({
					id: this.loginUser.user_id,
					phone: this.phone
				}).then(([err, data]) => {
					if (err) return;
					this.loginUser.phone = this.phone;
					this.$store.commit("setLoginUser", this.loginUser);
					this.$showToast('修改用户号码成功');
					this.$navigateBackDuration(1);
				})

			}
		}
	}
</script>

<style lang="scss">
	.updatePhone {
		.input-wrapper {
			padding:0 10px;
			height: 48px;
			line-height: 48px;
			background: rgba(0,0,0,0.08);
			border-radius: 8px 8px 8px 8px;
			display: flex;
			align-items: center;
			.desc {
				margin-right: 15px;
			}
			.line {
				width:1px;
				height: 15px;
				background-color: #333333;
				margin-right: 15px;			}
			.label {
				color: #06B7F3;
			}
			.input{
				padding-left: 0 !important;
				background: transparent !important;
				border:none !important;
			}
			.u-input{
				padding-left: 0 !important;
				background: transparent !important;
				border:none !important;
			}
			
			
		}
		.button-wrapper {
			position: fixed;
			bottom: 30px;
			width: 100%;

			.button-container {
				padding: 0upx 30upx;
			}

			.button {
				//background: #06B7F3;
				background: #6AD4F8;
				border-radius: 40upx;
				color: #fff;
				font-size: 14px;
				line-height: 44px;
				height: 44px;
				border:none;
			}
			.active-button{
				background: #06B7F3;
			}
			
		}

	}
</style>
